<?php
class Zendvn_Model_Table_Adv extends Zend_Db_Table{
	protected $_name = "adv";
	protected $_primary = "id"; 
	protected $_arrParam = array();
	
	public $_searchFields = array('name'=>'- Name field','code'=>'- Code field');
	
	protected $_listSQL;
	public function itemInSelect($arrParam = null,$options = null){
		$db = $this->getAdapter();
		if($options == null){
			$select = $db->select()->from($this->_name,array('id','name'))
						->where('status = "active"');
			$result = $db->fetchAll($select);
		}
		if($options['task'] == 'get-select'){
			$select = $db->select()->from($this->_name,array('id','name'))
						->where('status = "active"');
			$result = $db->fetchPairs($select);
		}
		return $result;
	}
	public function statusItem($arrParam = null,$options = null){
		if($options == null){
			
			$status = ($arrParam['s'] == 'active' )? 'inactive':'active';				
			$data = array('status'=>$status);
			$where = 'id = ' . (int)$arrParam['id'];
			$this->update($data, $where);	
		}
		
		if($options['task'] == 'multi'){
			$status = $arrParam['s'];				
			if(count($arrParam['cid'])>0){
				if($status == 'active'){
					$ids = implode(',', $arrParam['cid']);
					$where = 'id IN (' . $ids . ')';
					$data = array('status'=>$status);
					$this->update($data, $where);
				}else{
					foreach ($arrParam['cid'] as $val){						
						$where = 'id = ' . $val;
						$data = array('status'=>$status);
						$this->update($data, $where);						
					}
				}
			}
		}
	}
	
	public function deleteItem($arrParam = null,$options = null){
		$model = new Zendvn_Model_DeleteRecode();
		$model->user($arrParam,$options,$this);
	}
	public function getItem($arrParam = null,$options = null){
		
		if($options == null){
			$where = 'id = ' . (int)$arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
		}		
		if($options['task'] == 'base-alias'){
			$where = 'alias = "' . $arrParam['page'] . '"';
			$result = $this->fetchRow($where);
			if(!empty($result))
				$result = $result->toArray();
		}		
		return $result;
	}
	
	public function listItem($arrParam = null,$options = null){
		if($options == null){
			$db = $this->getAdapter();
			$select = $db->select()
						 ->from('adv AS i');
			
			
			$this->_listSQL = $select;
			$result = $db->fetchAll($select);		 
		}
		
		return $result;
	}
	
	public function totalItem($arrParam = null,$options = null){
		
		if($options == null){
			$db = $this->getAdapter();
			$select = $this->_listSQL->reset(Zend_Db_Select::COLUMNS);
			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_COUNT);
			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_OFFSET);
			$select = $this->_listSQL->reset(Zend_Db_Select::ORDER);
			$select = $this->_listSQL->reset(Zend_Db_Select::GROUP);
			$select->columns('COUNT(i.id) AS total');	
			$result = $db->fetchOne($select);
		}
		return $result;
		
	}
	
	public function saveItem($arrParam = null,$options = null){
		$arrParam['params'] = array("x1"=>$arrParam["x1"],"y1"=>$arrParam["y1"],"x2"=>$arrParam["x2"],"y2"=>$arrParam["y2"]);
		$row = $this->fetchNew();
		$row->link			= $arrParam['link'];			
		$row->params	= json_encode($arrParam['params']);
		
		$row->save();
	}
	

}